﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FWPDocumentation.Entities;

namespace FWPDocumentation
{
    public partial class Form3 : Form
    {
        private int nodeLevel = 0;
        private TreeNode nodeClicked;
        private TreeNodeCollection tnc;
        public Form3()
        {
            InitializeComponent();

        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if ((e.Node.Level < 3) && e.Button == MouseButtons.Right)
            {
                nodeLevel = e.Node.Level;
                contextMenuStrip1.Show(new Point(MousePosition.X, MousePosition.Y));
                tnc = e.Node.Nodes;
                nodeClicked = e.Node;
            }

        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            switch (nodeLevel)
            {
                case 0:
                    string userInput = EntryDialog.GetString("Название института");
                    var inst = Institute.CreateInstitute(userInput);
                    if (inst != null)
                    {
                        TreeNode node = new TreeNode(inst.name);
                        node.Tag = inst.idInstitute;
                        tnc.Add(node);

                    }
                    break;
                case 1:
                    userInput = EntryDialog.GetString("Название факультета");
                    var fac = Faculty.CreateFaculty(userInput, Convert.ToInt32(nodeClicked.Tag));
                    if (fac != null)
                    {
                        TreeNode node = new TreeNode(fac.nameFaculty);
                        node.Tag = fac.idFaculty;
                        tnc.Add(node);
                    }
                    break;
                case 2:
                    userInput = EntryDialog.GetString("Название группы");
                    var gr = Group.CreateGroup(userInput, Convert.ToInt32(nodeClicked.Tag));
                    if (gr != null)
                    {
                        TreeNode node = new TreeNode(gr.nameGroup);
                        node.Tag = gr.idGroup;
                        tnc.Add(node);
                    }
                    break;
            }
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            var insts = Institute.GetInstitutes();
            if (insts!= null)
            {
                TreeNode[] arrInst = new TreeNode[insts.Count];
                //TreeNodeCollection tree = treeView1.Nodes;
                for (int i = 0; i < insts.Count ; i++)
                {
                    var facs = insts.ElementAt(i).getFacultyes();
                    
                    TreeNode[] nodesFacs = new TreeNode[facs.Count];
                    for(int j=0; j<facs.Count ; j++)
                    {

                        var groups = facs.ElementAt(j).getGroups();
                        TreeNode[] nodesGr = new TreeNode[groups.Count];
                        for (int k = 0; k < groups.Count; k++)
                        {
                            nodesGr[k] = new TreeNode(groups.ElementAt(k).nameGroup);
                            nodesGr[k].Tag = groups.ElementAt(k).idGroup;
                        }
                        nodesFacs[j]=new TreeNode(facs.ElementAt(j).nameFaculty,nodesGr);
                        nodesFacs[j].Tag = facs.ElementAt(j).idFaculty;
                    }

                    TreeNode node = new TreeNode(insts.ElementAt(i).name,nodesFacs);
                    node.Tag = insts.ElementAt(i).idInstitute;
                    arrInst[i] = node;
                    //  tree.Add(node);
                }
                treeView1.Nodes.Add(new TreeNode("Институты", arrInst));

            }
        }


    }
}
